package com.darren.archive.entity;

import com.alibaba.fastjson.annotation.JSONField;
import com.darren.framework.entity.AppSession;
import com.darren.framework.entity.BaseEntity;
import com.darren.framework.utils.Constant;
import com.darren.framework.utils.SerializeUtil;
import org.hibernate.annotations.ResultCheckStyle;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.PostRemove;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.io.IOException;
import java.io.Serializable;
import java.util.Date;


@Entity
@Table(name = Constant.TABLE_NAME_PREV + "login_record")
@SQLDelete(sql = "update " + Constant.TABLE_NAME_PREV + "login_record set is_delete = true where id=?", check = ResultCheckStyle.COUNT)
@Where(clause = "is_delete  = false")
public class LoginRecord extends BaseEntity implements Serializable {

    private static final long serialVersionUID = 5810717084282992133L;

    /**
     * 登陆类型：USER 用户登陆,MEMBER 会员登陆
     */
    @Column(name = "login_type", columnDefinition = "varchar(100) default 'USER' comment '登陆类型：USER 用户登陆,MEMBER 会员登陆'")
    private String loginType;

    /**
     * 登陆者id
     */
    @Column(name = "login_id", columnDefinition = "bigint comment '登陆者id，用户id或者会员id'")
    private Long loginId;

    /**
     * 用户帐号，最大长度50个字符
     */
    @Column(name = "account", columnDefinition = "varchar(50) NOT NULL comment '用户帐号，最大长度50个字符'")
    private String userAccount;

    /**
     * 登陆时间
     */
    @JSONField(name = "loginTime", format = "yyyy-MM-dd HH:mm:ss")
    @Column(name = "login_time", columnDefinition = "datetime default CURRENT_TIMESTAMP comment '登陆时间'")
    private Date loginTime;

    /**
     * 过期时间，单位：毫秒，默认900000，15分钟
     */
    @Column(name = "expired_time", columnDefinition = "bigint default 900000 comment '过期时间，单位：毫秒，默认900000，15分钟'")
    private Long expiredTime;

    /**
     * application session
     */
    @Column(name = "app_session", columnDefinition = "longblob default null comment 'application session'")
    private byte[] appSession;

    /**
     * 登陆session
     */
    @Transient
    private AppSession session;

    public String getLoginType() {
        return loginType;
    }

    public void setLoginType(String loginType) {
        this.loginType = loginType;
    }

    public Long getLoginId() {
        return loginId;
    }

    public void setLoginId(Long loginId) {
        this.loginId = loginId;
    }

    public String getUserAccount() {
        return userAccount;
    }

    public void setUserAccount(String userAccount) {
        this.userAccount = userAccount;
    }

    public Date getLoginTime() {
        return loginTime;
    }

    public void setLoginTime(Date loginTime) {
        this.loginTime = loginTime;
    }

    public Long getExpiredTime() {
        return expiredTime;
    }

    public void setExpiredTime(Long expiredTime) {
        this.expiredTime = expiredTime;
    }

    public AppSession getSession() {
        return session;
    }

    public void setSession(AppSession session) {
        this.session = session;
    }

    public byte[] getAppSession() {
        return appSession;
    }

    public void setAppSession(byte[] appSession) throws ClassNotFoundException, IOException {
        if (appSession != null) {
            session = (AppSession) SerializeUtil.deserializeObject(appSession);
        }
        this.appSession = appSession;
    }

    public AppSession getApplicationSession() throws ClassNotFoundException, IOException {
        if (session == null && appSession != null) {
            session = (AppSession) SerializeUtil.deserializeObject(appSession);
        }
        return session;
    }

    public void setApplicationSession(AppSession session) throws IOException {
        appSession = SerializeUtil.serializeObject(session);
        this.session = session;
    }

    /**
     * 登陆类型
     *
     * @author Darren Zhou
     */
    public static enum LoginType {
        USER("USER", 1),
        MEMBER("MEMBER", 2),
        UNKNOWN("UNKNOWN", -9999);

        // 成员变量
        private String description;
        private int code;

        // 构造方法
        private LoginType(String description, int code) {
            this.description = description;
            this.code = code;
        }

        // 普通方法
        public static String getDescription(int code) {
            for (LoginType loginType : LoginType.values()) {
                if (loginType.getCode() == code) {
                    return loginType.description;
                }
            }
            return null;
        }

        // get set 方法
        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }

        public int getCode() {
            return code;
        }

        public void setCode(int code) {
            this.code = code;
        }
    }
}
